package top.yongyan0624.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.yongyan0624.common.lang.Result;
import top.yongyan0624.utils.OSSUtils;

/**
 * @author Yongyan
 * @description: TODO
 * @date 2021/8/15
 **/

@RestController
@RequestMapping("/img")
public class ImgController extends BaseController {

    @PostMapping("/upload")
    @PreAuthorize("hasRole('admin')")
    public Result modifyAvatar(MultipartFile img) {
        try {
            if (img.isEmpty()) {
                return Result.fail("图片路径为空");
            }
            //存储文件，同时获取图片URL
            String url = OSSUtils.getImgUrl(img);
            return Result.success(url);
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return Result.fail("图片上传失败！");
        }

    }
}
